using System;
using System.IO;
using System.Collections;
using System.Text;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace GeneratorZostav
{
    class Program
    {
        /// <summary>
        /// Vygeneruje help spravu ako sa program pouziva.
        /// </summary>
        /// <param name="sFileName">Menu suboru, ktore sa ma dostadit do textu.</param>
        /// <returns>Help text.</returns>
        private static string GetHelpText(string sFileName)
        {
            StringBuilder sbHelpText = new StringBuilder();
            sbHelpText.AppendLine("Pouzitie programu: " + sFileName + " [vstupny_subor [vystupny_subor]]");
            sbHelpText.AppendLine();
            sbHelpText.AppendLine("Ak nie su pouzite ziadne argumenty, program sa pokusi nacitat subor \"stext.txt\". Vystup bude vygenerovany ako \"stext.pdf\".");
            sbHelpText.AppendLine();
            sbHelpText.AppendLine("vstupny_subor: nazov vstupneho suboru. Ak nie je uvedeny druhy argument, vystupny subor bude vygenerovany s rovnakym menom a priponou \".pdf\".");
            sbHelpText.AppendLine();
            sbHelpText.AppendLine("vystupny_subor: nazov vystupneho suboru.");

            return sbHelpText.ToString();
        }

        /// <summary>
        /// Nastav nazvy vstupnych a vystupnych suborov.
        /// </summary>
        /// <param name="fin">Meno vstupneho subora.</param>
        /// <param name="fout">Meno vystupneho subora.</param>
        /// <param name="args">Pole argumentov funkcie Main().</param>
        private static void SetFileNames(out string fin, out string fout, string[] args)
        {
            // nastavenie mena vstupneho a vystupneho suboru
            // ak pouzivatel chce nieco specificke nazvy suborov, nastavime ich
            fin = "stext.txt";
            fout = "stext.pdf";

            if (args.Length > 0)
            {
                fin = args[0];
                if (args.Length > 1)
                    fout = args[1];
                else
                {
                    fout = fin.Substring(0, fin.LastIndexOf(".")) + ".pdf";
                }
            }
        }

        static void Main(string[] args)
        {
            // zobrazenie help spravy
            if (args.Length == 1 && (args[0] == "/?" || args[0] == "--help" || args[0] == "-h"))
            {
                Console.Write(GetHelpText("program.exe"));
                Console.ReadKey();
                return;
            }

            string sFileIn, sFileOut;
            // Nastav nazvy vstupnych a vystupnych suborov.
            SetFileNames(out sFileIn, out sFileOut, args);

            Console.WriteLine("Generator zostav, verzia 1.4");
            Console.Write("Generujem zostavu do suboru \"" + sFileOut + "\"...");
            
            try
            {
                CGeneratorZostavy oZostava = new CGeneratorZostavy(sFileIn, sFileOut);
                oZostava.Generuj();
                oZostava.Close();
                Console.WriteLine("hotovo!");
            }

            catch (Exception ex)
            {
                Console.WriteLine("\r\n\r\nChyba pri generovani pdf dokumentu: " + ex.Message);
            }
        }



          














        class Glossary : PdfPageEventHelper
        {

            // we will keep a glossary of words and the pages they appear on in a TreeMap
            SortedList glossary = new SortedList();

            // we override only the onGenericTag method
            public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)
            {
                glossary.Add(text, writer.PageNumber);
            }

            // we Add a method to retrieve the glossary
            public SortedList GetGlossary()
            {
                return glossary;
            }

        }


        private void oldmain()
        {
             Console.Write("Generujem PDF...");

            // nastavenie strany
            Rectangle pageSize = new Rectangle(144, 720);
            pageSize.BackgroundColor = new Color(0xFF, 0xFF, 0xDE);

            // vytvorenie dokumentu
            Document document = new Document(PageSize.A4, 36, 72, 108, 180);

            try
            {
                // prepojenie dokumentu a file streamu
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("pokus1.pdf", FileMode.Create));
                // nastavenie prehliadaca
                writer.ViewerPreferences = PdfWriter.PageLayoutTwoColumnLeft | PdfWriter.FitWindow | 
                                           PdfWriter.CenterWindow | PdfWriter.PageModeUseThumbs;
                
                // pridanie hlaviciek
                document.AddTitle("Pokusna zostava");
                document.AddAuthor("by Slavomir Hudak 2007");
                document.AddSubject("tlacena zostava");
                document.AddKeywords("zostava, meno zakaznika");
                document.AddCreator("Moj pokusny program, ktory vyuziva iText libku");

                // hlavicka
                HeaderFooter header = new HeaderFooter(new Phrase("Toto je hlavicka"), false);
                document.Header = header;

                // NEW PDF STARTs HERE
                document.Open();

                // nejaky text v dokumente
                document.Add(new Paragraph("paragraf1"));
                document.Add(new Paragraph("paragraf2"));
                for (int i = 0; i < 20; i++)
                {
                    document.Add(new Phrase("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People.X "));
                }


                // Zapatie, ale az od stranky c.2
                HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
                document.Footer = footer;

                // hlavicku vysmarime
                document.ResetHeader();

                document.NewPage();
                document.Add(new Paragraph("Hello Earth"));

                Font font = FontFactory.GetFont(FontFactory.COURIER, 12, Font.NORMAL);
                Chunk chunk = new Chunk("toto je kusok textu", font);
                document.Add(new Phrase(chunk));
                chunk = chunk.SetTextRise(6);
                document.Add(new Phrase(chunk));

                Phrase ph1 = new Phrase(24, "toto je fraza s leading (vzdialenost medzi riadkami) 24 toto je fraza s leading 24 toto je fraza s leading 24 toto je fraza s leading 24 toto je fraza s leading 24 toto je fraza s leading 24 toto je fraza s leading 24");
                document.Add(new Paragraph());
                document.Add(ph1);

                document.Add(new Phrase("What is the " + (char)945 + "-coefficient of the "
                                        + (char)946 + "-factor in the " + (char)947 + "-equation?\n"));
                for (int i = 913; i < 970; i++)
                {
                    document.Add(new Phrase(" " + i.ToString() + ": " + (char)i));
                }

                // PAGE 3
                document.NewPage();
                
                Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. ",
                    FontFactory.GetFont(FontFactory.HELVETICA, 10)));
                p1.Add("The leading of this paragraph is calculated automagically. ");
                p1.Add("The default leading is 1.5 times the fontsize. ");
                p1.Add(new Chunk("You can Add chunks "));
                p1.Add(new Phrase("or you can Add phrases. "));
                p1.Add(new Phrase("Unless you change the leading with the method setLeading, the leading doesn't change if you Add text with another leading. This can lead to some problems.", FontFactory.GetFont(FontFactory.HELVETICA, 18)));
                document.Add(p1);
                Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph. ",
                    FontFactory.GetFont(FontFactory.HELVETICA, 12)));
                p2.Add("As you can see, it started on a new line.");
                document.Add(p2);
                Paragraph p3 = new Paragraph("This is my third paragraph.",
                    FontFactory.GetFont(FontFactory.HELVETICA, 12));
                document.Add(p3);

                document.NewPage();

                // we create an Event and Add it to the writer
                Glossary pageEvent = new Glossary();
                writer.PageEvent = pageEvent;

                // we Add some content
                String[] f = new String[14];
                f[0] = "Courier";
                f[1] = "Courier Bold";
                f[2] = "Courier Italic";
                f[3] = "Courier Bold Italic";
                f[4] = "Helvetica";
                f[5] = "Helvetica bold";
                f[6] = "Helvetica italic";
                f[7] = "Helvetica bold italic";
                f[8] = "Times New Roman";
                f[9] = "Times New Roman bold";
                f[10] = "Times New Roman italic";
                f[11] = "Times New Roman bold italic";
                f[12] = "Symbol";
                f[13] = "Zapfdingbats";
                Font[] fonts = new Font[14];
                fonts[0] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.NORMAL);
                fonts[1] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD);
                fonts[2] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.ITALIC);
                fonts[3] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC);
                fonts[4] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL);
                fonts[5] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
                fonts[6] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.ITALIC);
                fonts[7] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC);
                fonts[8] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
                fonts[9] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);
                fonts[10] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC);
                fonts[11] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC);
                fonts[12] = FontFactory.GetFont(FontFactory.SYMBOL, 12, Font.NORMAL);
                fonts[13] = FontFactory.GetFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL);
                for (int i = 0; i < 14; i++)
                {
                    Chunk chunk2 = new Chunk("This is font ", fonts[i]);
                    Paragraph p = new Paragraph(chunk2);
                    p.Add(new Phrase(new Chunk(f[i], fonts[i]).SetGenericTag(f[i])));
                    document.Add(p);
                    if (i % 4 == 3)
                    {
                        document.NewPage();
                    }
                }

                // we Add the glossary
                document.NewPage();
                SortedList glossary = pageEvent.GetGlossary();
                foreach (string key in glossary.Keys)
                {
                    int page = (int)glossary[key];
                    Paragraph g = new Paragraph(key);
                    g.Add(" : page ");
                    g.Add(page.ToString());
                    document.Add(g);
                }


            }
            catch (DocumentException ex)
            {
                Console.Error.Write(ex.Message);
            }
            catch (IOException ex)
            {
                Console.Error.Write(ex.Message);
            }

            // zaverecne cistky
            document.Close();
            
            Console.WriteLine("DONE!");
        }
    }
}
